package com.lagou.sqlSession;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;
import com.lagou.pojo.SqlType;

import java.beans.IntrospectionException;
import java.lang.reflect.*;
import java.sql.SQLException;
import java.util.List;

public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementid, Object... params) throws IllegalAccessException, ClassNotFoundException, IntrospectionException, InstantiationException, SQLException, InvocationTargetException, NoSuchFieldException {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementid);
        List<Object> list = simpleExecutor.query(configuration, mappedStatement, params);
        return (List<E>) list;
    }

    @Override
    public int update(String statementId, Object... params) throws Exception {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        return simpleExecutor.update(configuration, mappedStatement, params);
    }

    @Override
    public <T> T selectOne(String statementid, Object... params) throws IllegalAccessException, IntrospectionException, InstantiationException, NoSuchFieldException, SQLException, InvocationTargetException, ClassNotFoundException {
        List<Object> objects = selectList(statementid, params);
        if (objects.size() == 1){
            return (T) objects.get(0);
        }else {
            throw new RuntimeException("查询结果为空/ 返回结果过多");
        }
    }


    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        // JDK 动态代理
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] args) throws Throwable {
                // 底层执行 JDBC 代码
                // 1 准备参数  statementid namespace.id  <=>    接口全限定名+ 方法名
                String methodName = method.getName();
                String ClassName = method.getDeclaringClass().getName();
                String statementid = ClassName +"." + methodName;

                // 参数2  params
                // 获取被调用方法的返回值类型
                /*Type genericReturnType = method.getGenericReturnType();

                // 判断是否类型参数化
                if (genericReturnType instanceof ParameterizedType ){
                    List<Object> list = selectList(statementid, args);
                    return list;
                }*/

                MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementid);
                //判断为 查询操作 还是 更新操作
                if (mappedStatement.getSqlType() != SqlType.SQL_QUERY) {
                    return update(statementid, args);
                }

                // 准备参数2：params:args
                // 获取被调用方法的返回值类型
                Type genericReturnType = method.getGenericReturnType();
                // 判断是否进行了 泛型类型参数化
                if(genericReturnType instanceof ParameterizedType){
                    List<Object> objects = selectList(statementid, args);
                    return objects;
                }



                return selectOne(statementid, args);
            }
        });
        return (T) proxyInstance;
    }

}
